package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tsms.domain.RouteInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.RoutePoint;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRltRouteDeviceMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRltRoutePostMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRouteInfoMapper;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRoutePointMapper;
import com.enjoyor.soa.traffic.core.tsms.service.IRouteInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.GeomtryHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class RouteInfoSerive implements IRouteInfoService{

	@Autowired
	private IRouteInfoMapper iRouteInfoMapper;
	
	@Autowired
	private IRoutePointMapper iRoutePointMapper;
	
	@Autowired
	private IPubLogMapper iPubLogMapper;
	
	@Autowired
	private IRltRouteDeviceMapper iRltRouteDeviceMapper;
	
	@Autowired
	private IRltRoutePostMapper iRltRoutePostMapper;

	@Override
	public Page getRouteInfoPage(Page page, String routeId, String routeName, String taskId, String routeStartPlace,
			String routeTargetPlace, String routeTypeId) {
		// TODO Auto-generated method stub
		Map<String,Object> mapBean = new HashMap<>();
		mapBean.put("routeId",routeId);
		mapBean.put("routeName",routeName);
		mapBean.put("taskId",taskId);
		mapBean.put("routeStartPlace",routeStartPlace);
		mapBean.put("routeTargetPlace",routeTargetPlace);
		mapBean.put("routeTypeId",routeTypeId);
		page.setMapBean(mapBean);
		return iRouteInfoMapper.selectPageInfo(page);
	}

	@Override
	public List<RouteInfo> getRouteInfoList(String routeId, String routeName, String taskId, String routeStartPlace,
			String routeTargetPlace, String routeTypeId) {
		// TODO Auto-generated method stub
		Map<String,Object> param = new HashMap<>();
		param.put("routeId",routeId);
		param.put("routeName",routeName);
		param.put("taskId",taskId);
		param.put("routeStartPlace",routeStartPlace);
		param.put("routeTargetPlace",routeTargetPlace);
		param.put("routeTypeId",routeTypeId);
		List<RouteInfo> routeInfoList = iRouteInfoMapper.getRouteInfoList(param);
		for(RouteInfo routeInfo : routeInfoList){
			String strGem = GeomtryHelper.getGeomToStringWithGson(routeInfo.getRouteGeo());
			routeInfo.setStrArea(strGem);
		}
		return routeInfoList;
	}

	@Override
	public String addRouteInfo(RouteInfo routeInfo,String personName) {
		// TODO Auto-generated method stub
		String routeId = "R"+ TimeHelper.getUniqueString();
		routeInfo.setCreateTime(TimeHelper.dateToLongString());
		routeInfo.setUpdateTime(TimeHelper.dateToLongString());
		routeInfo.setIsDelete(false);
		routeInfo.setRouteId(routeId);
		String taskId = routeInfo.getTaskId();
		String routeTypeId = routeInfo.getRouteTypeId();
		if(EnumDicPublic.ROUTE_EXECUTE.getKey().equals(routeTypeId)){
			//判断执行线路是否只有一条
			Map<String,Object> param = new HashMap<>();
			param.put("taskId",taskId);
			param.put("routeTypeId",routeTypeId);
			List<RouteInfo> routeInfoList = iRouteInfoMapper.getRouteInfoList(param);
			if(routeInfoList.size() > 0) {
				return "false";
			}
			routeInfo.setRouteTypeName(EnumDicPublic.ROUTE_EXECUTE.getValue());
		}else if(EnumDicPublic.ROUTE_STANDBY.getKey().equals(routeTypeId)){
			routeInfo.setRouteTypeName(EnumDicPublic.ROUTE_STANDBY.getValue());
		}else {
			return "false";
		}
		iRouteInfoMapper.insert(routeInfo);
		//生成途径点数据
		List<RoutePoint> routePointList = routeInfo.getRoutePointList();
		int pointSortno = 1;
		for(RoutePoint routePoint : routePointList){
			routePoint.setPointId("RO"+ TimeHelper.getUniqueString());
			routePoint.setRouteId(routeId);
			routePoint.setTaskId(taskId);
			routePoint.setPointSortno(pointSortno);
			if(routePoint.getParkTime() > (Double)0.0) {
				routePoint.setPointTypeId(EnumDicPublic.ROUTE_STOP.getKey());
			}
			String pointTypeId = routePoint.getPointTypeId();
			if(EnumDicPublic.ROUTE_START.getKey().equals(pointTypeId)) {
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_START.getValue());
			}else if(EnumDicPublic.ROUTE_PATHWAY.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_PATHWAY.getValue());
			}else if(EnumDicPublic.ROUTE_STOP.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_STOP.getValue());
			}else if(EnumDicPublic.ROUTE_TARGET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_TARGET.getValue());
			}else if(EnumDicPublic.ROUTE_BAYONET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_BAYONET.getValue());
			}else if(EnumDicPublic.ROUTE_CROSS.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_CROSS.getValue());
			}else {
				return "false";
			}
			iRoutePointMapper.insert(routePoint);
			pointSortno++;
		}
		//保存日志
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加线路:{%s} 操作成功!", new Object[] { routeInfo.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_ROUTE_INFO_MANAGEMENT.getMessage());
		return routeId;
	}

	@Override
	public Boolean editRouteInfo(RouteInfo routeInfo,String personName) {
		// TODO Auto-generated method stub
		Boolean flag = true;
		String routeId = routeInfo.getRouteId();
		routeInfo.setUpdateTime(TimeHelper.dateToLongString());
		//更新线路基本信息；
		iRouteInfoMapper.updateByIdSelective(routeInfo);
		//删除旧关联点；
		iRoutePointMapper.deleteByRouteId(routeId);
		//重新添加关联途径点；
		List<RoutePoint> routePointList = routeInfo.getRoutePointList();
		String taskId = routeInfo.getTaskId();
		int pointSortno = 1;
		for(RoutePoint routePoint : routePointList){
			routePoint.setPointId("RO"+ TimeHelper.getUniqueString());
			routePoint.setRouteId(routeId);
			routePoint.setTaskId(taskId);
			routePoint.setPointSortno(pointSortno);
			if(routePoint.getParkTime() > (Double)0.0) {
				routePoint.setPointTypeId(EnumDicPublic.ROUTE_STOP.getKey());
			}
			String pointTypeId = routePoint.getPointTypeId();
			if(EnumDicPublic.ROUTE_START.getKey().equals(pointTypeId)) {
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_START.getValue());
			}else if(EnumDicPublic.ROUTE_PATHWAY.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_PATHWAY.getValue());
			}else if(EnumDicPublic.ROUTE_STOP.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_STOP.getValue());
			}else if(EnumDicPublic.ROUTE_TARGET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_TARGET.getValue());
			}else if(EnumDicPublic.ROUTE_BAYONET.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_BAYONET.getValue());
			}else if(EnumDicPublic.ROUTE_CROSS.getKey().equals(pointTypeId)){
				routePoint.setPointTypeName(EnumDicPublic.ROUTE_CROSS.getValue());
			}else {
				flag = false;
				return flag;
			}
			iRoutePointMapper.insert(routePoint);
			pointSortno++;
		}
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改线路:{%s} 操作成功!", new Object[] { routeInfo.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_ROUTE_INFO_MANAGEMENT.getMessage());
		return flag;
	}

	@Override
	public void deleteRouteInfo(String routeId, String personName) {
		// TODO Auto-generated method stub
		//Map<String, Object> map = new HashMap<>();
		//map.put("routeId", routeId);
		//删除线路关联信息；
		iRoutePointMapper.deleteByRouteId(routeId);
		iRltRoutePostMapper.delPostByRouteId(routeId);
		iRltRouteDeviceMapper.deleteRltRouteDeviceByRouteId(routeId);
		//删除线路信息；
		iRouteInfoMapper.deleteById(routeId);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
	                String.format("删除常备线路:{%s} 操作成功!", new Object[] { routeId }),
	                LogInfoDic.LOG_MODULAR_ENUM.TSMS_COMMON_ROUTE_MANAGEMENT.getMessage());
	}

	@Override
	public void changeRouteType(String taskId, String routeId, String personName) {
		// TODO Auto-generated method stub
		//把执行线路改为备用
		this.changeTypeToStandby(taskId);
		//把备用改成执行线路
		RouteInfo routeInfoB = new RouteInfo();
		routeInfoB.setRouteId(routeId);
		routeInfoB.setRouteTypeId(EnumDicPublic.ROUTE_EXECUTE.getKey());
		routeInfoB.setRouteTypeName(EnumDicPublic.ROUTE_EXECUTE.getValue());
		routeInfoB.setUpdateTime(TimeHelper.dateToLongString());
		iRouteInfoMapper.updateByIdSelective(routeInfoB);
	}
	
	@Override
	public void changeTypeToStandby(String taskId) {
		Map<String, Object> param = new HashMap<>();
		param.put("taskId", taskId);
		param.put("routeTypeId", EnumDicPublic.ROUTE_EXECUTE.getKey());
		List<RouteInfo> routeInfoList = iRouteInfoMapper.getRouteInfoList(param);
		for(RouteInfo routeInfo : routeInfoList) {
			RouteInfo routeInfoA = new RouteInfo();
			routeInfoA.setRouteId(routeInfo.getRouteId());
			routeInfoA.setRouteTypeId(EnumDicPublic.ROUTE_STANDBY.getKey());
			routeInfoA.setRouteTypeName(EnumDicPublic.ROUTE_STANDBY.getValue());
			routeInfoA.setUpdateTime(TimeHelper.dateToLongString());
			iRouteInfoMapper.updateByIdSelective(routeInfoA);
		}
	}
	@Override
	public List<RouteInfo> getRouteProcress(String ececuteDate, String taskName, String beforeLicensePlate,
			String schemeId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("ececuteDate", ececuteDate);
		map.put("taskName", taskName);
		map.put("beforeLicensePlate", beforeLicensePlate);
		map.put("schemeId", schemeId);
		List<RouteInfo> list = iRouteInfoMapper.getRouteProcress(map);
		//String strgem = JSONArray.toJSONString(list.get(0).getRouteGeo(),SerializerFeature.DisableCircularReferenceDetect);
		//地理信息Json转string
		for(int i = 0;i<list.size();i++){
			String strgem = GeomtryHelper.getGeomToStringWithGson(list.get(i).getRouteGeo());
			list.get(i).setStrArea(strgem);
		}
		return list;
	}

}
